package com.lemon.exam.common.util;

import java.util.UUID;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;

/**
 * 类对象
 *
 * @author Lemon
 * @since 2025/4/11 16:23
 */
public class RandomUtil {
    private static final char[] ALL_CHAR = {
            '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };

    private static final char[] LOW_CASE_CHAR = {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    };

    private static final char[] UPPER_CASE_CHAR = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };

    private static final char[] NUM_CHAR = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };

    // 选择高性能的 LXM 算法实现
    private static final RandomGenerator RANDOM = RandomGeneratorFactory.of("L64X128MixRandom").create();

    /**
     * 生成随机字符串（包含字母和数字）
     *
     * @param length
     * @return
     */
    public static String all(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(ALL_CHAR[RANDOM.nextInt(ALL_CHAR.length)]);
        }
        return sb.toString();
    }

    /**
     * 生成随机字符串（只包含数字）
     *
     * @param length
     * @return
     */
    public static String num(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(NUM_CHAR[RANDOM.nextInt(NUM_CHAR.length)]);
        }
        return sb.toString();
    }

    /**
     * 生成随机字符串（只包含小写字母）
     *
     * @param length
     * @return
     */
    public static String lowCase(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(LOW_CASE_CHAR[RANDOM.nextInt(LOW_CASE_CHAR.length)]);
        }
        return sb.toString();
    }

    /**
     * 生成随机字符串（只包含大写字母）
     *
     * @param length
     * @return
     */
    public static String upperCase(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(UPPER_CASE_CHAR[RANDOM.nextInt(UPPER_CASE_CHAR.length)]);
        }
        return sb.toString();
    }

    /**
     * 获取UUID
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString();
    }
}
